package com.fangmao.a04_retrofit;

import android.content.res.Configuration;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
requestLayout
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/")
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .build();

        /**
         * create:动态代理
         * 创建一个类
         * 实现接口
         * 接口中每个方法都会实现同样的逻辑，这些方法会调用InvocationHandler.invoke
         */
        GitHubService api = retrofit.create(GitHubService.class);
        api.listRepos("rengwuxian").enqueue(new Callback<List<Repo>>() {
            @Override
            public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
                List<Repo> repos = response.body();
            }

            @Override
            public void onFailure(Call<List<Repo>> call, Throwable t) {
            }
        });

        okhttpDemo();
    }

    /**
     * 从tcp连接建立、TLS连接建立、HTTP报文组装和传输、各种HTTP特性的支持（重试、跳转、cache、cookie、content-length）
     * 接管了整个过程，随便把API做的更舒服一点
     */
    private void okhttpDemo() {
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.newCall(new Request.Builder()
                .url("https://api.github.com/")
                .build())
                .enqueue(new okhttp3.Callback() {
                    @Override
                    public void onFailure(okhttp3.Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {

                    }
                });
    }
}
